package com.luiz.aero.android.sensor;

import java.util.List;

import com.luiz.aero.android.data.StaticPositionData;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class AccelerometerListener  implements SensorEventListener{
	/** Called when the activity is first created. */
	int x,y,z;
	private Sensor mAccelerometer;
	private SensorManager sm;
	private TextView acelerometerSensorTx;


	public AccelerometerListener(SensorManager sm, TextView tx) {
		super();
		List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);
		if (sensors.size() > 0) //dispositivo android tiene acelerometro
		{
			sm.registerListener(this, sensors.get(0), SensorManager.SENSOR_DELAY_GAME);
		}
		
		acelerometerSensorTx = tx;

	}

	protected void onPause()
	{
		sm.unregisterListener(this, mAccelerometer);
	}

	protected void onStop()
	{
		sm.unregisterListener(this, mAccelerometer);
	}


	@Override
	public void onSensorChanged(SensorEvent event) {
		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
			this.x =  (int)event.values[SensorManager.DATA_X];
			this.y =  (int)event.values[SensorManager.DATA_Y];
			this.z =  (int)event.values[SensorManager.DATA_Z];
			float[] accValues = this.getValues();
			acelerometerSensorTx.setText("X: " + accValues[0] + "Y: " + accValues[1] + "Z: " + accValues[2]);
			
			StaticPositionData.accelerationX = this.x;
			StaticPositionData.accelerationY = this.y;
			StaticPositionData.accelerationZ = this.z;
		}
	}


	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {	
	}
	
	public float[] getValues(){
		float[] a = {x, y, z};
		return a;
		
	}
}
